1Writer タスクの開始・終了 3
いくつかのパターンで開始時刻の打刻ができない問題を解消
開始・終了のテスト用
code:md
テキスト
- テキスト
-
- テキスト
-
- 16:24- テキスト
- 16:24-
code:js
const now = new Date().toLocaleTimeString("ja-JP", { hour: "2-digit", minute: "2-digit" });
// 現在のカーソル位置を取得
const initialCursorPos = editor.getSelectedRange().location;
const currentLineRange = editor.getSelectedLineRange();
const currentLineText = editor.getTextInRange(currentLineRange0, currentLineRange1); // 行が空白、または "- " のみの場合、新しいタスクを作成
if (!currentLineText.trim() || currentLineText.trim() === "-") {
const newTask = - [ ] ${now}- ;
editor.replaceTextInRange(currentLineRange0, currentLineRange1, newTask); editor.setSelectedRange(currentLineRange0 + newTask.length); return;
}
// "- テキスト" の場合も開始時刻を追加
if (/^- (?!\[ x\])/.test(currentLineText)) { const updatedLine = currentLineText.replace(/^-\s/, - [ ] ${now}-);
editor.replaceTextInRange(currentLineRange0, currentLineRange1, updatedLine); // カーソルをカーソル行の末尾に移動
editor.setSelectedRange(currentLineRange0 + updatedLine.length); }
// 1. のみ、または テキスト の場合: 開始時刻を追加
else if (/^- \ \(?! \d{2}:\d{2}-)/.test(currentLineText)) { const updatedLine = currentLineText.replace("- ", - [ ] ${now}-);
editor.replaceTextInRange(currentLineRange0, currentLineRange1, updatedLine); // カーソルをカーソル行の末尾に移動
editor.setSelectedRange(currentLineRange0 + updatedLine.length); }
// 2. テキストのみの場合: 新しいタスクとして時刻を追加
else if (/^^\-/.test(currentLineText)) { const newTask = - [ ] ${now}- ${currentLineText};
editor.replaceTextInRange(currentLineRange0, currentLineRange1, newTask); // カーソルをカーソル行の末尾に移動
editor.setSelectedRange(currentLineRange0 + newTask.length); }
// 3. hh:mm- の場合: 終了時刻を追加して、完了状態にする
else if (/^- \ \ \d{2}:\d{2}-/.test(currentLineText)) { const updatedLine = currentLineText.replace(/^-\s\ \\s(\d{2}:\d{2})-/, - [x] $1-${now}); editor.replaceTextInRange(currentLineRange0, currentLineRange1, updatedLine); // カーソルを次の行に設定
editor.setSelectedRange(currentLineRange0 + updatedLine.length + 1); }
// 4. x hh:mm-hh:mm の場合: 行を複製し、複製した行を未完了状態にして、hh:mm-hh:mm の部分を消す else if (/^- \x\ \d{2}:\d{2}-\d{2}:\d{2}/.test(currentLineText)) { const timeRangeEnd = currentLineText.indexOf(" ", 16);
const taskDescription = currentLineText.slice(timeRangeEnd).trim();
const newLine = - [ ] ${taskDescription};
// 現在の行を保持し、複製した行を追加
editor.replaceTextInRange(currentLineRange0, currentLineRange1, currentLineText); editor.replaceTextInRange(currentLineRange1, currentLineRange1, "\n" + newLine); // カーソルを次の行の末尾に設定
editor.setSelectedRange(currentLineRange1 + newLine.length + 1); }